package com.ebraga.trackerclub.android.handler.core;

import com.ebraga.trackerclub.android.dao.entity.ParameterAndroid;
import com.ebraga.trackerclub.android.exception.BackofficeDBConnectionException;
import com.ebraga.trackerclub.android.exception.handler.BackofficeHandlerException;
import com.ebraga.trackerclub.android.exception.manager.BackofficeIllegalArgumentManagerException;
import com.ebraga.trackerclub.android.exception.manager.BackofficeManagerException;
import com.ebraga.trackerclub.android.handler.api.ParameterHandler;
import com.ebraga.trackerclub.android.manager.api.ParameterManager;
import com.ebraga.trackerclub.android.util.DLog;
import com.ebraga.trackerclub.android.vo.ParameterVO;

public abstract class ParameterHandlerImpl implements ParameterHandler {
	
	protected ParameterManager parameterManager;

	@Override
	public ParameterVO getParameter(String param) throws BackofficeHandlerException {
		
		try{
			ParameterAndroid parameterAndroid = parameterManager.getParameter(param);
			ParameterVO parameterVO = null;
			
			if (parameterAndroid != null) {
				parameterVO = new ParameterVO();
				parameterVO.setDescription(parameterAndroid.getDescription());
				parameterVO.setName(parameterAndroid.getName());
				parameterVO.setValue(parameterAndroid.getValue());
			}
			
			return parameterVO;
			
		}catch(BackofficeIllegalArgumentManagerException e){
			DLog.e(e);
			throw new BackofficeHandlerException("Usuario invalido"); 
		} catch (BackofficeDBConnectionException e) {
			DLog.e(e);
			throw new BackofficeHandlerException("Erro ao conectar banco de dados");
		} catch (BackofficeManagerException e) {
			DLog.e(e);
			throw new BackofficeHandlerException("Erro ao recuperar usuario");
		}
		
	}
		
	
	protected void setParameterManager(ParameterManager parameterManager) {
		this.parameterManager = parameterManager;
	}
	
	
}
